<?php
namespace App\Service\Admin;

use App\Service\BaseService as Service;
use Illuminate\Support\Facades\DB;

class IndexService extends Service
{
    /**
     * 后台登录
     * @param string $uname 账户名
     * @param string $upwd 密码
     */
    public function dologin($uname, $upwd)
    {
        $resOne = $this->dbQueryOne($this->dbAdmin, ['name' => $uname]);
        if (empty($resOne)) {
            return array('code' => -2, 'msg' => '账号不存在');
        }
        if ($resOne['passwd'] != md5($upwd)) {
            return array('code' => -2, 'msg' => '密码错误');
        }
        //返回值
        $resData = [
            'adminid' => $resOne['id'],
            'adminName' => $resOne['name'],
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $resData);
    }

    //后台菜单
    public function getLeftMenu()
    {
        $res = $this->dbQueryList($this->dbAction, ['del' => 0, 'pid' => 0], [], [], ['sort' => 'desc']);
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有菜单');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $childArys = [];
            $childOnes = $this->dbQueryList($this->dbAction,
                ['del' => 0, 'pid' => $v['id']], [],
                ['id' => 'id', 'title' => 'title', 'link' => 'link']);
            if (count($childOnes)) {
                foreach ($childOnes as $vv) {
                    $vv = (array) $vv;
                    $childArys[] = [
                        'id' => $vv['id'],
                        'title' => $vv['title'],
                        'link' => $vv['link'],
                    ];
                }
            }
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'icon' => $v['icon'],
                'link' => $v['link'],
                'childs' => $childArys,
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    //后台功能列表
    public function getActionIndex()
    {
        $db = DB::table($this->dbAction)
            //->where('del', 0)
            ->where('pid', 0);
        $total = $db->count();
        $res = $db->select('id', 'title', 'link', 'pid', 'del')->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有菜单');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $childArys = [];
            $childOnes = DB::table($this->dbAction)
                ->select('id', 'title', 'link', 'pid', 'del')
                //->where('del', 0)
                ->where('pid', $v['id'])
                ->get();
            $total ++;
            if (count($childOnes)) {
                foreach ($childOnes as $vv) {
                    $vv = (array) $vv;
                    $childArys[] = [
                        'id' => $vv['id'],
                        'title' => $vv['title'],
                        'link' => $vv['link'],
                        'pid' => $vv['pid'],
                        'pname' => $v['title'],
                        'del' => $vv['del'],
                    ];
                    $total ++;
                }
            }
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'link' => $v['link'],
                'pid' => 0,
                'pname' => '-',
                'del' => $v['del'],
                'childs' => $childArys,
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    //后台功能一个
    public function getActionOne($id)
    {
        $res = (array) DB::table($this->dbAction)
            ->select('id', 'title', 'link', 'pid')
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '没有菜单');
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    //获取父级
    public function getParent()
    {
        $res = DB::table($this->dbAction)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('pid', 0)
            ->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有菜单');
        }
        $arys[] = [ 'id' => 0, 'title' => '顶层功能' ];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 保存更新
     * @param int $id
     * @param array $pas
     */
    public function actionUpdate($id, $pas)
    {
        $res = (array) DB::table($this->dbAction)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => 'no data');
        }
        $pas['uptime'] = toDate();
        $up = DB::table($this->dbAction)->where('id', $id)->update($pas);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 删除
     */
    public function actionDel($id, $del)
    {
//        $res = (array) DB::table($this->dbAction)
//            ->where('del', 0)
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return array('code' => -2, 'msg' => '没有记录');
//        }
        $pas = [
            'del' => $del,
            'deltime' => toDate(),
        ];
        $up = $this->dbConn($this->dbAction)->where('id', $id)->update($pas);
        if (!$up) {
            return array('code' => -2, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }
}